package com.example.student.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.student.entity.Admin;
import com.example.student.entity.LoginForm;
import com.example.student.entity.Router;
import com.example.student.entity.Teacher;
import com.example.student.mapper.AdminMapper;
import com.example.student.mapper.RouterMapper;
import com.example.student.mapper.TeacherMapper;
import com.example.student.mapper.UserMapper;
import com.example.student.utils.ResponseResult;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 登录
 *
 * @author 17754
 */
@RestController
public class LoginController {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private UserMapper userMapper;

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private TeacherMapper teacherMapper;

    @Resource
    private RouterMapper routerMapper;

    private List<Map<String, Object>> formatList(List<Map<String, Object>> list){
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            if (map.get("router_type").equals(0)) {
                for (int j = i + 1; j < list.size(); j++) {
                    Map<String, Object> childMap = list.get(j);
                    if (map.get("id").equals(childMap.get("parent_id"))) {
                        // 将子路由添加到父路由的 children 中
                        List<Map<String, Object>> children = (List<Map<String, Object>>) map.getOrDefault("children", new ArrayList<>());
                        children.add(childMap);
                        map.put("children", children);
                        // 使用索引删除
                        list.remove(j);
                        // 调整索引以适应删除操作
                        j--;
                    }
                }
            }
        }
        return list;
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public ResponseResult login(@RequestBody LoginForm form) {

        QueryWrapper<LoginForm> loginQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();

        QueryWrapper<Router> routerQueryWrapper = new QueryWrapper<>();

        String account = form.getAccount();
        String password = form.getPassword();
        String role = form.getRole();
        String code = form.getCode();

        if (code.equals(redisTemplate.opsForValue().get("code"))){
                /* 验证码通过  */
            if (role.equals("管理员")){
                adminQueryWrapper.eq("account", account)
                        .eq("password", password);
                if (adminMapper.selectList(adminQueryWrapper).isEmpty()){
                    return ResponseResult.fail("账号或密码错误!");
                }else{
                    routerQueryWrapper
                            .eq("router_status", 1)
                            .and(wrapper -> wrapper.eq("router_role", 0)
                                    .or().eq("router_role", 1));
                    List<Map<String, Object>> list = formatList(routerMapper.selectMaps(routerQueryWrapper));
                    return ResponseResult.success("登录成功", list);
                }
            }
            if (role.equals("学生")){
                loginQueryWrapper.eq("account", account)
                        .eq("password", password);
                if (userMapper.selectList(loginQueryWrapper).isEmpty()){
                    return ResponseResult.fail("账号或密码错误!");
                }else{
                    routerQueryWrapper
                            .eq("router_status", 1)
                            .and(wrapper -> wrapper.eq("router_role", 3)
                                    .or().eq("router_role", 0));
                    List<Map<String, Object>> list = formatList(routerMapper.selectMaps(routerQueryWrapper));
                    return ResponseResult.success("登录成功", list);
                }
            }
            if (role.equals("教师")){
                loginQueryWrapper.eq("account", account)
                        .eq("password", password);
                if (teacherMapper.selectList(teacherQueryWrapper).isEmpty()){
                    return ResponseResult.fail("账号或密码错误!");
                }else{
                    routerQueryWrapper
                            .eq("router_status", 1)
                            .and(wrapper -> wrapper.eq("router_role", 0)
                                    .or().eq("router_role", 2));
                    List<Map<String, Object>> list = formatList(routerMapper.selectMaps(routerQueryWrapper));
                    return ResponseResult.success("登录成功", list);
                }
            }

        }else{
            return ResponseResult.fail("验证码错误或已过期!");
        }
        return ResponseResult.fail("登录失败,请联系维护人员!");
    }
}
